<template>
	
	<dialog-shell title="用户登录" confirm-text="登录" ref="login" @confirm="confirmLogin">
		<image src="/static/icons(1)/icon.png" class="icon-login"></image>
		<text class="welcome-text">WELCOME</text>
		<text class="info-register" @click="clickRegister">没有账号？请注册</text>
		<view class="user">
			<input type="text" :value="userName" @input="inputGetUser"  placeholder="请输入用户名" placeholder-class="input-holder"/>
		</view>
		<view class="password">
			<input v-if="isPwd" type="password" :value="password" @input="inputGetPwd" placeholder="请输入密码" placeholder-class="input-holder" />
			<input v-else="isPwd" type="text" :value="password" @input="inputGetPwd"  placeholder="请输入密码" placeholder-class="input-holder" />
		</view>
		
	</dialog-shell>
	
</template>

<script>
	export default {
		name:"login-dialog",
		data() {
			return {
				isPwd:true,
				userName:"",
				password:""
			};
		},
		methods:{
			clickRegister(){
			  uni.navigateTo({
			  	url: '/pages/register/register',
			  	success: res => {
					this.$refs.login.hideDialog()
				},
			  	fail: () => {},
			  	complete: () => {}
			  });	
			},
			confirmLogin(){
			  	if(this.userName.length==0||this.password.length==0){
					uni.showToast({
						title: '请输入用户名密码',
						icon:'none'
					});
					return
				}
				let url= this.$params.host+this.$params.action_login
				let data={
					username:this.userName,
					password:this.password
				}
				console.log(data);
				uni.showLoading({
					title: '登录中...',
					mask: false
				});
				this.$req.post(url,data,res=>{
					if(!res.data.success){
						uni.showToast({
							title: res.data.message,
							icon:'none'
						});
						this.show()
						return
					}
					let app=getApp()
					app.globalData.id=res.data.data.id
					app.globalData.token=res.data.data.token
					app.globalData.avatar=res.data.data.avatar
					app.globalData.email=res.data.data.email
					app.globalData.nickName=res.data.data.nickName
					app.globalData.userName=res.data.data.userName
					app.globalData.type=res.data.data.type
					this.$noti.post(this.$params.noti_login_staus)
					console.log(app.globalData);
					this.$emit("login")
				},()=>{
					uni.hideLoading()
				})
			},
			inputGetPwd(e){
			   this.password=e.detail.value	
			},
			inputGetUser(e){
			   this.userName=e.detail.value	
			},
			switchPwd(){
			 this.isPwd=!this.isPwd	
			},
		    show(){
				this.$refs.login.showDialog()
			},
			hide(){
				this.$refs.login.hideDialog()
			}
		}
	}
</script>

<style>
@import url("login-dialog.css");
</style>